package zhoufu.sz.sanxiao_check.ui;

import android.content.Context;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.inject.HasComponent;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.ui.fragment.StreetListFragment;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/27
 */
public class SearchActivity extends BaseActivity implements HasComponent<LoginComponet>
{

    LoginComponet mComponet;

    private int Type = 0;

    @Override protected void initInject()
    {
        setTitle(R.string.title_search);
        mComponet = DaggerLoginComponet.builder().activityModule(getActivityModule())
                .appComponent(getAppComponent()).build();
    }

    @Override protected int setIndividualId()
    {
        return R.layout.layout_frame;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem         menuItem   = menu.findItem(R.id.menu_search);
        final SearchView searchView = (SearchView) menuItem.getActionView();
        menuItem.expandActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
        {
            @Override public boolean onQueryTextSubmit(String query)
            {
                if ( searchView != null )
                {
                    InputMethodManager imm = (InputMethodManager) getSystemService(
                            Context.INPUT_METHOD_SERVICE);
                    if ( imm != null )
                    {
                        imm.hideSoftInputFromWindow(
                                searchView.getWindowToken(), 0);
                    }
                    searchView.clearFocus();
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                switch (Type)
                {
                    case 0:
                        transaction.replace(R.id.main_fram,
                                StreetListFragment.newInstance(query, "0", "0", "0"));
                        break;
                    case 1:
                        transaction.replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", query, "0", "0"));
                        break;
                    case 2:
                        transaction.replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", query, "0"));
                        break;
                    case 3:
                        transaction.replace(R.id.main_fram,
                                StreetListFragment.newInstance("0", "0", "0", query));
                        break;
                }
                transaction.commitAllowingStateLoss();
                return true;
            }

            @Override public boolean onQueryTextChange(String newText)
            {
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override public boolean onOptionsItemSelected(MenuItem item)
    {
        if ( item.getItemId() == R.id.menu_search_type )
        {
            showFilteringPopUpMenu(item);
        }
        return super.onOptionsItemSelected(item);
    }

    public void showFilteringPopUpMenu(final MenuItem mitem)
    {
        PopupMenu popup = new PopupMenu(this, findViewById(R.id.menu_search_type));
        popup.getMenuInflater().inflate(R.menu.popmenu_search, popup.getMenu());
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
        {
            public boolean onMenuItemClick(MenuItem item)
            {
                switch (item.getItemId())
                {
                    case R.id.menu_search_1:
                        Type = 0;
                        break;
                    case R.id.menu_search_2:
                        Type = 1;
                        break;
                    case R.id.menu_search_3:
                        Type = 2;
                        break;
                    case R.id.menu_search_4:
                        Type = 3;
                        break;
                }
                mitem.setTitle(item.getTitle());
                return true;
            }
        });
        popup.show();
    }

    @Override public LoginComponet getComponent()
    {
        return mComponet;
    }
}
